﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace SportingGoodsExhibitionSystem
{
    /// <summary>
    /// Regist.xaml 的交互逻辑
    /// </summary>
    public partial class Regist : Window
    {
        public Regist()
        {
            InitializeComponent();
        }
        string strCon = "server=.;database=CDB;user=sa;pwd=123456";
        public bool InsertInfo(int stuNum, string passWord)
        {
            try
            {
                SqlConnection con = new SqlConnection(strCon);
                SqlCommand cmd = new SqlCommand("insert into [student](studentNumber,password)  values(@username,@password)", con);
                cmd.Parameters.Add("@username", SqlDbType.Int, 10).Value = stuNum;
                cmd.Parameters.Add("@password", SqlDbType.VarChar, 10).Value = passWord;
                con.Open();
                int i = cmd.ExecuteNonQuery();
                con.Close();
                if (i > 0)
                    return true;
            }
            catch
            {
                MessageBox.Show("学号已存在");
            }
            
            return false;
        }
        public bool InsertInfoStu(string stuName, string stuNum, string stuCollege)
        {
            try
            {
                SqlConnection con = new SqlConnection(strCon);
                SqlCommand cmd = new SqlCommand("insert into [studentInfo](studentNumber,studentName,studyCollege,leaveTimes)  values(@num,@name,@college,0)", con);
                cmd.Parameters.Add("@num", SqlDbType.Int, 10).Value = stuNum;
                cmd.Parameters.Add("@name", SqlDbType.VarChar, 10).Value = stuName;
                cmd.Parameters.Add("@college", SqlDbType.VarChar, 10).Value = stuCollege;
                con.Open();
                int i = cmd.ExecuteNonQuery();
                con.Close();
                if (i > 0)
                    return true;
                
            }
            catch
            {

            }
           
            return false;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
            if (passWord1.Password.Equals(passWord2.Password) == true)
            {
                if (InsertInfo(Convert.ToInt32(stuNumber.Text), passWord1.Password) == true && InsertInfoStu(textBox1.Text, stuNumber.Text, studyCollege.Text) == true)
                {
                    MessageBox.Show("注册成功,点击关闭按钮返回主界面");
                }
                else
                {
                    stuNumber.Text = "";
                }
            }else
            {
                MessageBox.Show("两次密码不一致，请重新输入");
            }
            }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

            MainWindow mw = new MainWindow();
            mw.Show();
            this.Close();
        }
    }
       

}


 

